$.extend($.fn.validatebox.defaults.rules, {
    CHS:  {
        validator:  function (value, param) {
            return /^[\u0391-\uFFE5]+$/.test(value);
        },
        message:  '请输入汉字'
    },
    ZIP:  {
        validator:  function (value, param) {
            return /^[1-9]\d{5}$/.test(value);
        },
        message:  '邮政编码不存在'
    },
    QQ:  {
        validator:  function (value, param) {
            return /^[1-9]\d{4,10}$/.test(value);
        },
        message:  'QQ号码不正确'
    },
    mobile:  {
        validator:  function (value, param) {
            return /^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$/.test(value);
        },
        message:  '手机号码不正确'
    },
    loginName:  {
        validator:  function (value, param) {
            return /^[\u0391-\uFFE5\w]+$/.test(value);
        },
        message:  '登录名称只允许汉字、英文字母、数字及下划线。'
    },
    safepass:  {
        validator:  function (value, param) {
            return safePassword(value);
        },
        message:  '密码由字母和数字组成，至少6位'
    },
    equalTo:  {
        validator:  function (value, param) {
            return value == $(param[0]).val();
        },
        message:  '两次输入的字符不一至'
    },
    number:  {
        validator:  function (value, param) {
            return /^\d+$/.test(value);
        },
        message:  '请输入数字'
    },
    idcard:  {
        validator:  function (value, param) {
            return idCard(value);
        },
        message: '请输入正确的身份证号码'
    },
	strDate: { 
		validator: function(value, param){           
			return isDate(value);
		}, 
		message:'请输入正确的日期<div style="padding-left:8px">(yyyy-MM-dd)</div>'
	},
	strTime: { 
		validator: function(value, param){           
			return isTime(value);
		}, 
		message:'请输入正确的时间<div style="padding-left:8px">(HH:mm:ss)</div>'
	},
	strDateTime: { 
		validator: function(value, param){           
			return isDateTime(value);
		}, 
		message:'请输入正确的日期时间<div style="padding-left:8px">(yyyy-MM-dd HH:mm:ss)</div>'
	},
	isEmail: {
		validator: function(value, param){
		return isEmail(value);
		},
		message:'请输入正确的邮件地址!'
	},
	unique: {
        validator: function(value, param){
        	var result = false;
        	//param[0]校验URL
        	//param[1]校验字段参数名称
        	//param[2]修改时校验记录IDS
        	//param[3]校验URL与字段参数的链接符号，如 ?、&等URL参数链接字符
        	var url = param[0]+(!param[3]?'?':param[3])+param[1]+'='+value+(param[2]?'&'+param[2]+'='+$('#'+param[2]).val():'');
        	$.ajax({
        		type: "GET",
        		dataType:"json",
                url:url,
                async: false,
                success: function(data){
                	result=eval(data);
                }
        	});
            return result;
        },
        message: '输入项已存在'
    }
});

;(function($){
	$.extend({
		fWindow:function(options){
		    var defaults = {
		        title: '窗口标题',        //窗口标题
		        width: 668,                //宽度
		        height: 450,            //高度
		        shadow: true,
		        minimizable: false,        //最小化
		        maximizable: false,        //最大化
		        closed: true,
		        draggable:true,
		        resizable: false,        //改变窗口大小
		        modal: true,            //模态    
		        iconCls: 'icon-save',          //图标class
	    		onClose:function(){
	                window.setTimeout(function(){$(win).window('destroy',false);},  0);
	            }  
		    };
		    var options = $.extend(defaults,options);
		    var winid = options.winid;
		    var html = options.html;
		    var url = options.url;
		    var btn = options.btn;
		    if(!html)
		    	html =$.createFrame(url);
		    
		    var win = top.document.createElement("div");
		    win.id=winid;
		    win.style.cssText = "padding:5px;background:#fafafa;";
		    top.document.body.appendChild(win); 
		    top.$(win).window({
		    		title:options.title,
		    		width:options.width,
		    		height:options.height,
		    		content:html,
		    		shadow: options.shadow,
		    		collapsible:options.collapsible,
		    		minimizable:options.minimizable,
		    		maximizable:options.maximizable,
		    		closed:options.closed,
		    		draggable:options.draggable,
		    		maximizable:options.maximizable,
		    		resizable:options.resizable,
		    		modal:options.modal,
		    		iconCls:options.iconCls
		    }).window('open');

		 }
	});
})(jQuery);

;(function($){
	$.extend({
		closeFWin:function(winid){
			window.setTimeout(function(){top.$('#'+winid,top.document.body).window('close');},  0);
		}
	});
})(jQuery);

;(function($){
	$.extend({
		rereshFrameData:function(dataGridId){
			var frameLen = top.frames.length;
			for(var i=0;i<frameLen;i++){
				var mainFrame = top.frames[i];
				if(mainFrame){
					var gridId = mainFrame.$(dataGridId);
					if(gridId&&gridId.length==1){
						gridId.datagrid("reload");
						break;
					}
				}
			}
		}
	});
})(jQuery);
;(function($){
	$.extend({
		rereshSubFrameData:function(dataGridId){
			var frameLen = top.frames.length;
			for(var i=0;i<frameLen;i++){
				var mainIframe = top.frames[i];
				if(mainIframe){
					var subiFrames = mainIframe.frames;
					if(subiFrames){
						var isBreak = false;
						var subFrameLen = subiFrames.length;
						for(var j=0;j<subFrameLen;j++){
							var subIframe = subiFrames[j];
							if(subIframe&&subIframe.location!='about:blank'){
								var gridId = subIframe.$(dataGridId);
								if(gridId&&gridId.length==1){
									gridId.datagrid("reload");
									isBreak = true;
									break;
								}
							}
						}
						if(isBreak)break;
					}
				}
			}
		}
	});
})(jQuery);
;(function($){
	$.extend({
		rereshFrameTreeData:function(dataGridId,url){
			var frameLen = top.frames.length;
			for(var i=0;i<frameLen;i++){
				var mainFrame = top.frames[i];
				if(mainFrame){
					var gridId = mainFrame.$(dataGridId);
					if(gridId&&gridId.length==1){
						//gridId.treegrid("reload");
						var node = gridId.treegrid('getSelected');
						if (node){
							var PNode = gridId.treegrid('getParent',node.ids);
							if(!PNode){
								gridId.treegrid('options').url = url;
							}
							gridId.treegrid('reload',PNode?PNode.ids:null);
						}else{
							if(url)
								gridId.treegrid('options').url = url;
							gridId.treegrid('reload');
						}
						break;
					}
				}
			}
		}
	});
})(jQuery);

;(function($){
	$.extend({
		createFrame:function(url){
			return '<iframe scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;height:100%;"></iframe>';
		}
	});
})(jQuery);

;(function($){
	$.extend({
		 format:function(s, n){     
		     n = n > 0 && n <= 20 ? n : 2;     
		     s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";     
		     var l = s.split(".")[0].split("").reverse(),     
		     r = s.split(".")[1];     
		     t = "";     
		     for(i = 0; i < l.length; i ++ ){     
			 t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");     
		     }     
		     return t.split("").reverse().join("") + "." + r;     
		 }
	});
})(jQuery); 
;(function($){
	$.extend({    
	    uniqueArray:function(a) {     
	        var r=[];     
	        for (var i=0;i<a.length;i++){
	        	if($.inArray(a[i],r)<0)r.push(a[i]); 
	        }
	        return r;     
	     }     
	});
})(jQuery);
;(function($){
	$.extend({
		pFloat:function(s){
			if(!s)
				return 0;
			else
				return parseFloat((s + "").replace(/[^\d\.-]/g, ""));     
		}  
	});
})(jQuery);

/* 密码由字母和数字组成，至少6位 */
var safePassword = function (value) {
    return !(/^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/.test(value));
};
var isEmail=function(value) {
	var r=value.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/);
	if(r==null)
		return false; 
	return true;
};

var idCard = function (value) {
    if (value.length == 18 && 18 != value.length) return false;
    var number = value.toLowerCase();
    var d, sum = 0, v = '10x98765432', w = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2], a = '11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91';
    var re = number.match(/^(\d{2})\d{4}(((\d{2})(\d{2})(\d{2})(\d{3}))|((\d{4})(\d{2})(\d{2})(\d{3}[x\d])))$/);
    if (re == null || a.indexOf(re[1]) < 0) return false;
    if (re[2].length == 9) {
        number = number.substr(0, 6) + '19' + number.substr(6);
        d = ['19' + re[4], re[5], re[6]].join('-');
    } else d = [re[9], re[10], re[11]].join('-');
    if (!isDateTime(d)) return false;
    for (var i = 0; i < 17; i++) sum += number.charAt(i) * w[i];
    return (re[2].length == 9 || number.charAt(17) == v.charAt(sum % 11));
};
/* 短时间，形如 (13:04:06) */
var isTime = function(str) { 
	var a = str.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/); 
	if (a == null) {
		 return false;
	} 
	if (a[1]>24 || a[3]>60 || a[4]>60) {  
		return false 
	} 
	return true; 
}; 
/* 短日期，形如 (1999-01-01) */
var isDate = function(str) { 
	var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/); 
	if(r==null)
		return false; 
	var d= new Date(r[1], r[3]-1, r[4]); 
	return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]); 
}; 
/* 长时间，形如 (1999-01-01 13:04:06) */
var isDateTime = function(str) { 
	var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/; 
	var r = str.match(reg); 
	if(r==null)
		return false; 
	var d= new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]); 
	return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7]); 
};
//弹出信息窗口 title:标题 msgString:提示信息 msgType:信息类型 [error,info,question,warning]
function msgShow(title, msgString, msgType) {
	$.messager.alert(title,'<div style="padding-top:15px;padding-left:5px"><b>'+msgString+'</b></div>', msgType);
}
var formatter = function(date){
    var y = date.getFullYear();
    var m = date.getMonth()+1;
    var d = date.getDate();
    var h = date.getHours();
    var M = date.getMinutes();
    var s = date.getSeconds();
    function formatNumber(value){
    	return (value < 10 ? '0' : '') + value;
    }
    return y+'-'+m+'-'+d+' '+ formatNumber(h)+':'+formatNumber(M)+':'+formatNumber(s);
};
var parser = function(s){
    var t = Date.parse(s);
	   if (!isNaN(t)){
		   return new Date(t);
	   } else {
		   return new Date();
	}
};

var transTreeCHK = function(treeData,value){
	if(value){
		var values = value.split(',');
		for(var i=0; i<treeData.length; i++){
			if(treeData[i].checked==true)
				treeData[i].checked = false;
			if(treeData[i].isLeaf==false){
			    var children =treeData[i].children;
			    if(children&&children.length>0){
					childrenCHK(children,values);
			    }
			}else{
				if(values.length>0){
					for(var k=0;k<values.length;k++){
						if(treeData[i].id==values[k])
							treeData[i].checked=true;
					}
				}else{
					if (treeData[i].id == value)
						treeData[i].checked=true;
				}
			}
		}
	}
	return treeData;
};
var childrenCHK = function(inChildren,values){
	for(var i=0;i<inChildren.length;i++){
		if(inChildren[i].checked==true)
			inChildren[i].checked = false;
		if(inChildren[i].isLeaf==false){
		    var subChildren =inChildren[i].children;
		    if(subChildren&&subChildren.length>0)
		    	childrenCHK(subChildren,values);
		}else{
			if(values.length>0){
				for(var k=0;k<values.length;k++){
					if(inChildren[i].id==values[k])
						inChildren[i].checked=true;
				}
			}else{
				if (inChildren[i].id == values)
					inChildren[i].checked=true;
			}
		}
	}
};
var loadDataName = function(inData,value){
	for(var i=0; i<inData.length; i++){
		if (inData[i].ids == value) return inData[i].names;
	}
	return value;
};
var loadOneTreeText = function(treeData,value){
	var texts = "";
	if(value){
		for(var i=0; i<treeData.length; i++){
			if(treeData[i].isLeaf==false){
			    var children =treeData[i].children;
			    if(children&&children.length>0){
			    	texts = childrenOneText(children,value);
			    }
			}else{
				if (treeData[i].id == value){
					texts= treeData[i].text;
				}
			}
		}
		return texts;
	}
	return value;
};
var childrenOneText = function(inChildren,value){
	var texts = "";
	for(var i=0;i<inChildren.length;i++){
		if(inChildren[i].isLeaf==false){
		    var subChildren =inChildren[i].children;
		    if(subChildren&&subChildren.length>0)
		    	texts = childrenOneText(subChildren,value);
		}else{
			if (inChildren[i].id == value){
				texts = inChildren[i].text;
			}
		}
	}
	return texts;
};
var loadTreeText = function(treeData,value){
	var texts = "";
	if(value){
		var values = value.split(',');
		for(var i=0; i<treeData.length; i++){
			if(treeData[i].isLeaf==false){
			    var children =treeData[i].children;
			    if(children&&children.length>0){
			    	texts += childrenText(children,values);
			    }
			}else{
				if(values.length>0){
					for(var k=0;k<values.length;k++){
						if(treeData[i].id==values[k]){
							if (texts != "") texts += ',';
							texts += treeData[i].text;
						}
					}
				}else{
					if (treeData[i].id == value) texts += treeData[i].text;
				}
			}
		}
		return texts;
	}
	return value;
};
var childrenText = function(inChildren,values){
	var texts = "";
	for(var i=0;i<inChildren.length;i++){
		if(inChildren[i].isLeaf==false){
		    var subChildren =inChildren[i].children;
		    if(subChildren&&subChildren.length>0)
		    	texts +=childrenText(subChildren,values);
		}else{
			if(values.length>0){
				for(var k=0;k<values.length;k++){
					if(inChildren[i].id==values[k]){
						if (texts != "") texts += ',';
						texts += inChildren[i].text;
					}
				}
			}else{
				if (inChildren[i].id == values) texts += inChildren[i].text;
			}
		}
	}
	return texts;
};